shape3D = [141, 1051, 601]; % arranged in the order of dimx, dimy, dimz, the size of input file
target_z = 300;             % slice the data according to the user's demand, one can also slice dimX or dimY
path_to_3D = "F:\Sea_small_server_z302_t1200.dat";
is_raw = true;


fp = fopen(path_to_3D, "rb");

if is_raw
   [wave3D, num] = fread(fp, shape3D, "double");
else
    [wave3D, num] = fread(fp, shape3D, "short");
end

fclose(fp);

% do slicing here
wave2D = wave3D(:, :, target_z);            % the last dimension is dimz
wave2D = abs(wave2D);


max_val = max(max(max(wave3D)));
temp_wave2D = uint8(wave2D / max_val * 255);

new_file_name = strrep(path_to_3D, ".dat", ".png");  % todo: special naming could lead to inconsistent file names
imwrite(temp_wave2D, new_file_name);
